package com.ai.zhly.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.base.impl.jotmBase.BaseMServiceImpl;
import com.ai.dmc.index.base.sequence.SequenceGenerator;
import com.ai.dmc.index.common.InitParam;
import com.ai.dmc.index.common.bean.AreaBean;
import com.ai.dmc.index.common.bean.CommonBaseBean;
import com.ai.dmc.index.common.bean.ExportBean;
import com.ai.dmc.index.common.bean.FuncBean;
import com.ai.dmc.index.common.bean.SceneMonitorBean;
import com.ai.dmc.index.common.bean.SmsBean;
import com.ai.dmc.index.common.cache.AreaCache;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.index.common.util.DateUtil;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import com.ai.zhly.service.ISceneMonitorService;
import com.ai.zhly.util.DBSequence;
import com.ai.zhly.util.sms.client.SPClient;
import com.mchange.v1.io.InputStreamUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
* @Title: MapDataImpl.java
* @Description: 家装宽带展示层service
* @author xueyf3  
* @date 2016年5月23日 下午4:22:54
* Copyright：Copyright (c) 2016
* Company：北京亚信智慧数据科技有限公司
* @version V1.0
 */
public class FuncServiceImpl extends BaseServiceImpl implements IFuncService{
	// 定义对应Mapper文件的命名空间
    private static final String NAMESPACE = "Func.";
	public final static String SMS_SEND_SUCCESS = "1"; // 短信发送：成功
	public final static String SMS_SEND_FAIL = "0"; // 短信发送：失败
	
    public static Logger log = LoggerFactory.getServiceLog(FuncServiceImpl.class);
    
    private ICommonService commonService;
    private IFuncUtilService funcUtil;
    private ILogService logService;
	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}
	
	public void setFuncUtil(IFuncUtilService funcUtil) {
		this.funcUtil = funcUtil;
	}
	
	public void setLogService(ILogService logService) {
		this.logService = logService;
	}

	/**
	 * 
	 * @Description :功能新增
	 * @author xueyf3
	 * @date 创建时间：2015年8月18日 下午5:26:23
	 * @version 1.0
	 */
	@Override
	public void funcAdd(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		FuncBean bean = new FuncBean();
		Boolean rFlag = false;
		String funcId = inputObject.getParams().get("funcId");
		String funcType = inputObject.getParams().get("funcType");
		String funcShow = inputObject.getParams().get("funcShow");
		String funcUrlMore = inputObject.getParams().get("funcUrlMore");
		String status  = inputObject.getParams().get("status");
		String isVisible = inputObject.getParams().get("isVisible");
		
		String parentFuncId = inputObject.getParams().get("parentFuncId");
		String funcName = inputObject.getParams().get("funcName");
		String funcUrl = inputObject.getParams().get("funcUrl");
		String funcOrder = inputObject.getParams().get("funcOrder");
		bean.setFuncId(funcId);
		bean.setFuncName(funcName);
		bean.setParentFuncId(parentFuncId);
		bean.setFuncUrl(funcUrl);
		bean.setFuncOrder(funcOrder);
		bean.setAdminId(inputObject.getParams().get("userId"));
		bean.setAdminName(inputObject.getParams().get("userId"));
		bean.setAdminTime(DateUtil.getInstance().getToday("yyyyMMddHHmmss"));
		Boolean check = funcUtil.checkOnlyId(funcId);
		resultMap.put("checkResult",check);
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
		if (!check) { // false 已存在
			return;
		}
		
		if ("0".equals(funcType)) {
			// 验证funcId是否已存在
			// 添加到功能文件夹
			bean.setFuncShow(funcShow);
			rFlag = funcUtil.addFuncFolder(bean);
		} else {
			// 添加到功能列表
			funcUrlMore = funcUrlMore.trim();
			bean.setFuncUrlMore(funcUrlMore);
			bean.setStatus(status);
			bean.setIsVisible(isVisible);
			rFlag = funcUtil.addFuncObj(bean);
		}
		
		resultMap.put("data",rFlag);
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}
	/**
	 * 
	 * @Description :删除所选的功能
	 * @author xueyf3
	 * @date 创建时间：2015年8月20日 下午3:38:21
	 * @version 1.0
	 */
	@Override
	public void funcDelete(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String funcIds = inputObject.getParams().get("funcIds");

		Boolean rFlag = funcUtil.deleteFunc(funcIds);
		
		resultMap.put("data",rFlag);
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}
  /**
	 * 
	 * @Description :修改功能
	 * @author xueyf3
	 * @date 创建时间：2015年8月20日 上午11:47:09
	 * @version 1.0
	 */
	@Override
	public void funcModify(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		FuncBean bean = new FuncBean();
		Boolean rFlag = false;
		String funcId = inputObject.getParams().get("funcId");
		String funcType = inputObject.getParams().get("funcType");
		String funcShow = inputObject.getParams().get("funcShow");
		String funcUrlMore = inputObject.getParams().get("funcUrlMore");
		String status  = inputObject.getParams().get("status");
		String isVisible = inputObject.getParams().get("isVisible");
		
		String parentFuncId = inputObject.getParams().get("parentFuncId");
		String funcName = inputObject.getParams().get("funcName");
		String funcUrl = inputObject.getParams().get("funcUrl");
		String funcOrder = inputObject.getParams().get("funcOrder");
		bean.setFuncId(funcId);
		bean.setFuncName(funcName);
		bean.setParentFuncId(parentFuncId);
		bean.setFuncUrl(funcUrl);
		bean.setIsVisible(isVisible);
		bean.setFuncOrder(funcOrder);
		bean.setAdminId(inputObject.getParams().get("userId"));
		bean.setAdminName(inputObject.getParams().get("userId"));
		bean.setAdminTime(DateUtil.getInstance().getToday("yyyyMMddHHmmss"));
		if ("0".equals(funcType)) {
			// 添加到功能文件夹
			bean.setFuncShow(funcShow);
			rFlag = funcUtil.modifyFolder(bean);
		} else {
			// 添加到功能列表
			funcUrlMore = funcUrlMore.trim();
			bean.setFuncUrlMore(funcUrlMore);
			bean.setStatus(status);
			bean.setIsVisible(isVisible);
			rFlag = funcUtil.modifyFunc(bean);
		}
		
		resultMap.put("data",rFlag);
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}
 /**
	 * 
	 * @Description : 查询某个功能信息
	 * 
	 * @author xueyf3
	 * @date 创建时间：2015年8月19日 下午4:27:05
	 * @version 1.0
	 */
	@Override
	public void queryFunc(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String funcId = inputObject.getParams().get("funcId");
		FuncBean bean = funcUtil.queryFunc(funcId);
		JSONObject j = JSONObject.fromObject(bean);
		resultMap.put("data",j);
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
		
	}
	/**
	 * 查询功能树
	 */
	@Override
	public void queryFuncTree(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String userId = inputObject.getParams().get("userId");
		boolean isAdmin = inputObject.getParams().get("isAdmin")==null?false:Boolean.valueOf(inputObject.getParams().get("isAdmin"));
		JSONArray jArr = funcUtil.queryFuncTree(userId, isAdmin);
		
		resultMap.put("data",jArr);
		outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}

	
}
